<?php

/**
 * RbacForm class.
 * RbacForm is the data structure for keeping
 * web Ad form data. It is used by the 'rbac' action of 'RbacController'.
 */
class RbacForm extends CFormModel
{
    public $name;
    public $type;
    public $description;
    public $ruleName;
    public $data;
    public $route;
    public $method;
    public $group;
    public $category;
    public $sort;
    public $permissions;
    public $roles;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// permission
            array('route, method, description, group, category', 'required', 'on' => 'permission'),
            array('sort', 'numerical', 'integerOnly'=>true, 'on'=>array('role', 'permission')),
            array('sort', 'default', 'value'=>0, 'on'=>array('role', 'permission')),
            array(
                'route',
                'match',
                'pattern' => '/^[\/].*/',
                'message' => Yii::t('app', Yii::t('app', 'Must begin with "/" like "/module/controller/action" format')),
                'on' => 'permission'
            ),

            array('name, description', 'required', 'on' => 'role'),
            array('roles, permissions', 'default', 'on' => 'role'),

			//array('id,value','safe'),

		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
            'route' => Yii::t('app', 'Route'),
            'method' => Yii::t('app', 'HTTP Method'),
            'description' => Yii::t('app', 'Description'),
            'group' => Yii::t('app', 'Group'),
            'category' => Yii::t('app', 'Category'),
            'sort' => Yii::t('app', 'Sort'),
            'name' => Yii::t('app', 'Role'),
            'permissions' => Yii::t('app', 'Permissions'),
            'roles' => Yii::t('app', 'Role'),
		);
	}

    public static function getPermissionsByGroup($type='index')
    {
        $authManager = Yii::app()->getAuthManager();
        $fillDatas = [];
        $originPermissions = $authManager->getAuthItem();

        $permissions = [];
        foreach ($originPermissions as $originPermission){
            $data = json_decode($originPermission->data, true);
            $temp = explode(":", $originPermission->name);
            $permissions[] = [
                'name' => $originPermission->name,
                'route' => $temp[0],
                'method' => $temp[1],
                'description' => $originPermission->description,
                'group' => $data['group'],
                'category' => $data['category'],
                'sort' => $data['sort'],
            ];
        }
        array_multisort($permissions, 'sort');
        foreach ($permissions as $permission){
            $fillDatas[$permission['group']][$permission['category']][] = $permission;
        }
        $return = [];
        if( $type == 'index' ){
            foreach ($fillDatas as $value){
                foreach ($value as $val) {
                    foreach ($val as $v) {
                        $return[] = new self(array_merge($v, ['scenario' => 'role']));
                    }
                }
            }
        }else{
            $return = $fillDatas;
        }
        return $return;
    }


}
